#! /bin/bash
### BEGIN INIT INFO
# Provides:          lance_tool
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: Start lance_tool
# Description: Start and Stop the lance_tool
### END INIT INFO
#
# Lance Tool
#
# chkconfig: 345 20 80
# description: Lance Tool
#
# /etc/rc.d/init.d/lance_*
# See how we were called.

DEVICE=ppp0
PPPCNF="Lance_VPN"
setup_nat=""

function vpn_conn(){
    nohup /usr/sbin/pppd call "$PPPCNF" >/dev/null 2>&1 &

    sleep 5
    $setup_nat client
    if [ $? -eq 0 ]
    then
        return 0
    else
        return 1
    fi
}

function vpn_dscn(){
    if [ -r /var/run/$DEVICE.pid ]
    then
        kill -INT `cat /var/run/$DEVICE.pid`
        if [ !"$?" = "0" ]
        then
            rm -rf /var/run/$DEVICE.pid
            # echo "ERROR: Removed stale pid file"
            return 1
        fi
        # echo "PPP link to $DEVICE terminated."
        return 0
    fi

    # echo "ERROR: PPP link is not active on $DEVICE"
    return 1
}


function ck_itf(){
  interface_test=$(ifconfig | grep "ppp0 ")
  if [ "$interface_test" != "" ]
  then
    #echo "[OK]: VPN interface exists"
    return 0
  else
    #echo "[ER]: VPN not found, reconnecting......"
    vpn_dscn
    vpn_conn
    return 1
  fi
}

function ck_link(){
  link_test=$(ping -c 3 192.168.11.3 | grep "time=")
  if [ "$link_test" != "" ]
  then
    #echo "[OK]: Gateway is online"
    return 0
  else
    #echo "[ER]: Gateway is offline, reconnecting......"
    vpn_dscn
    vpn_conn
    return 1
  fi
}

function vpn_proc_pid(){
  ps -elf | grep "`basename $0`" | grep background | grep -v grep | awk '{print $4}' | head -n 1
}

case "$1" in
    start)
        nohup $0 background >/dev/null 2>&1 &
        sleep 3
        PID=`vpn_proc_pid`
        echo "Started `basename $0` [$PID]"
        exit 0
    ;;

    stop)
        PID=`vpn_proc_pid`
        if [ "$PID" != "" ]
        then
            kill -s 9 $PID
            sleep 3
            vpn_dscn
            echo "Stopped `basename $0` [$PID]"
        else
            echo "Nothing to do."
        fi
        exit 0
    ;;

    restart)
        $0 stop
        $0 start
        exit 0
    ;;

    status)
        PID=`vpn_proc_pid`
        [ "$PID" != "" ] && echo "$PPPCNF Daemon Process [$PID]"
        [ -r /var/run/$DEVICE.pid ] && echo "$PPPCNF PPTP Connect Process [`cat /var/run/$DEVICE.pid`]"
        ifconfig | grep "P-t-P:" | awk '{gsub(/^ */,"",$0);print "'"$PPPCNF"'"" "$0;}'
        exit 0
    ;;

    background)
        while [ 1 ]
        do
            ck_itf
            sleep 10
            ck_link
            sleep 10
        done
        exit 0
    ;;

    install)
        CUR_DIR="`pwd`"
        cd "`dirname $0`"
        SCR_PATH="`pwd`"
        setup_nat="$SCR_PATH"/setup_nat.sh
        cat `basename $0` | awk '{\
          if ($0 ~ /^setup_nat=.+/){\
            print "setup_nat=""'"$setup_nat"'";\
          } else {\
            print $0;\
          }\
        }' > /etc/init.d/`basename $0`
        chmod +x /etc/init.d/`basename $0`
        chkconfig `basename $0` on 2>/dev/null
        chkconfig `basename $0` on 2>/dev/null
        cd "$CUR_DIR"
        exit 0
    ;;

    *)
        echo "Usage: /etc/init.d/`basename $0` {start|stop|restart|status}"
        exit 1
    ;;
esac


